package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    private final EmployeeService employeeService;
    private final JwtProperties jwtProperties;
    @Autowired
    public EmployeeController(EmployeeService employeeService,
                              JwtProperties jwtProperties){
        this.employeeService = employeeService;
        this.jwtProperties = jwtProperties;
    }


    /**
     * 新增员工
     *
     * @param employeeDTO 员工DTO
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<?> saveEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("执行 新增员工 接口:{}", employeeDTO);
        System.out.println("当前线程id：" + Thread.currentThread().getId());
        return Result.success();
    }

    /**
     * 根据id删除员工
     * @param employeeId id
     * @return Result
     */
    @DeleteMapping("/{employeeId}")
    public Result<?> deleteEmployee(@PathVariable Long employeeId){
        log.info("执行 删除员工 接口:{}",employeeId);
        employeeService.deleteEmployee(employeeId);
        return Result.success();
    }

    /**
     * 修改员工信息
     * @param employeeDTO 员工实体类DTO
     * @return Result
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result<?> updateEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("执行 修改员工信息 接口:{}",employeeDTO);
        employeeService.updateEmployee(employeeDTO);
        return Result.success();
    }


    /**
     * 开启或者禁用员工账号
     * @param status 账号状态
     * @param employeeId 员工id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation("开启或者禁用员工账号")
    public Result<?> startOrStopEmployee(@PathVariable Integer  status,@RequestParam("id") Long employeeId){
        log.info("执行 启用禁用员工账号 接口：{},{}",status,employeeId);
        employeeService.startOrStopEmployee(status,employeeId);
        return Result.success();
    }

    /**
     * 员工分页查询数据
     * @param employeePageQueryDTO 员工查询DTO
     * @return Result
     */
    @GetMapping("/page")
    @ApiOperation("分页查询员工数据")
    public Result<PageResult> pageQueryEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("执行 员工分页查询 接口:{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQueryEmployee(employeePageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据id插叙员工信息
     * @param employeeId 员工id
     * @return Result
     */
    @GetMapping("/{employeeId}")
    @ApiOperation("查询员工")
    public Result<Employee> getEmployeeById(@PathVariable Long employeeId){
        log.info("执行 根据id查询员工信息 接口：{}",employeeId);
        Employee employee = employeeService.getEmployeeById(employeeId);
        return Result.success(employee);
    }


    /**
     * 登录
     *
     * @param employeeLoginDTO 员工DTO
     * @return Result
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> loginEmployee(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("执行 员工登录 接口：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return Result
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<?> logoutEmployee() {
        log.info("执行 员工退出 接口");
        return Result.success();
    }




}